# -*- coding:utf-8 -*-
"""
@Time : 2025/05/06 21:47 PM
@Author: linfeng
@Des: 栏目列表
"""
from datetime import date
import os
import time

from app.common.common import write_access_log
from core.Response import success, fail, res_antd
from models.base import Content, Category, ModelNews
from schemas.portal import category
from fastapi import HTTPException, Request, Query, APIRouter, Security, File, UploadFile
from config import settings
from typing import List
from tortoise.queryset import F
from core.Utils import strip_tags

router = APIRouter(prefix='')

@router.get("/nice", summary="内容列表", )
async def nice_list(category_id: int, limit: int = 6, isnice: int = 1):
    """
    获取内容列表
    :return:
    """
    query = {}
    if not category_id:
        raise HTTPException(status_code=404, detail="栏目不能为空")
    cate_data = await Category.filter(cate_id=category_id).first()
    if not cate_data:
        raise HTTPException(status_code=404, detail="栏目不存在")
    
    
    
    query.setdefault('category', category_id)
    if isnice:
        query.setdefault('isnice', isnice)
    
    content_data = await Content.filter(**query).limit(limit).all().order_by("-sort").values('id', 'title', 'create_time')
    for item in content_data:
        item['create_time'] = item['create_time'].strftime("%Y-%m-%d")
        if cate_data.cate_type == 1:
            extra_data = await ModelNews.filter(con=item['id']).first()
            item['description'] = strip_tags(extra_data.content)[:3*9].strip() + '...'

    return success(data=content_data)

